<?php
/**
 * Http请求
 * @App	
 * @Group 
 * @Action HttpRequestService
 * @author lilei
 * @date 2018年10月12日
 *
 */
namespace app\common\service;

use app\common\library\CommonFun;
use Requests;
use Requests_Response;

class HttpRequestService {
	
	/**
	 * 请求地址
	 * @var string
	 */
	var $request_url = '';
	
	/**
	 * 请求头信息
	 * @var array
	 */
	var $request_headers = [];
	
	/**
	 * 返回头信息
	 * @var array
	 */
	var $response_headers = [];
	
	/**
	 * Request Option 配置
	 * @var array
	 */
	var $request_options = [];
	
	/**
	 * 请求返回信息
	 * @var int|string
	 */
	var $response_http_code;
	
	/**
	 * 请求是否成功
	 * @var string
	 */
	var $request_success = FALSE;
	
	/**
	 * 返回结果实体
	 */
	var $response_body;
	
	/**
	 * 返回对象
	 * @var \Requests_Response
	 */
	var $response;
	
	/**
	 * 构造函数
	 */
	public function __construct() {
		// 注册Requests
		Requests::register_autoloader();
	}
	
	/**
	 * 返回单例模型
	 * @return static
	 */
	public static function getInstance() {
		$param = func_get_args ();
		return CommonFun::get_instance_of ( get_called_class (), '', $param );
	}
	
	/**
	 * 设置Request Option 配置
	 * @param mixed $name 配置名称
	 * @param mixed $value 配置值
	 * @return HttpRequestService
	 */
	public function SetRequestOptions( $name, $value ) {
		$this->request_options[$name] = $value;
		return $this;
	}
	
	/**
	 * 设置代理
	 * @param string $ip		IP地址
	 * @param int|string $port	端口
	 * @param string $username	用户名
	 * @param string $password	密码
	 */
	public function SetProxy( $ip, $port, $username='', $password='' ) {
		$proxy = $ip.":".$port;
		if ( $username ) {
			$value = [
					$proxy, $username, $password
			];
		}else {
			$value = $proxy;
		}
		$this->SetRequestOptions("proxy", $value);
	}
	
	/**
	 * get请求
	 * @param string $url	请求地址
	 * @param array $param	请求参数
	 * @return HttpRequestService
	 */
	public function HttpGet( $url, array $param=[] ) {
		if (!empty($param)) {
			$url = $url . '?' . http_build_query($param);
		}
		$this->request_url = $url;
		try {
			$response = Requests::get($url, $this->request_headers, $this->request_options);
		} catch (\Exception $e) {
			$response = new Requests_Response();
			$response->body = $e->getMessage(); 
		}
		
		return $this->parseResponse($response);
	}

    /**
     * post请求
     * @param string $url 请求地址
     * @param array $postData 请求参数
     * @param bool $applicationJson 是否为application/Json 请求
     * @return HttpRequestService
     */
	public function HttpPost( $url, array $postData, $applicationJson=FALSE ) {
		if ( $applicationJson ) {
			$postData = json_encode($postData);
		}
		$this->request_url = $url;
		try {
			$response = Requests::post($url, $this->request_headers, $postData, $this->request_options);
		} catch (\Exception $e) {
			$response = new Requests_Response();
			$response->body = $e->getMessage(); 
		}
		
		return $this->parseResponse($response);
	}
	
	/**
	 * 获取原始返回的Body实体数据
	 * @return mixed
	 */
	public function GetResponseOriBody() {
		if ( $this->request_success ) {
			return $this->response_body;
		}else {
			return false;
		}
	}
	
	/**
	 * 获取JsonBody转为数组后的实体数据
	 * @return array|boolean
	 */
	public function GetResponseJsonToArrayBody() {
		if ( $this->request_success ) {
			return json_decode($this->response_body, true);
		}else {
			return false;
		}
	}
	
	/**
	 * 解析返回信息
	 * @param Requests_Response $response
	 * @return HttpRequestService
	 */
	public function parseResponse( Requests_Response $response ) {
		$this->response = $response;
		// 返回状态码
		$this->response_http_code = $response->status_code;
		// 返回头信息
		$this->response_headers = $response->headers->getAll();
		// 返回实体数据
		$this->response_body = $response->body;
		// 请求是否成功
		$this->request_success = $response->success;
		
		return $this;
	}
	
}